package com.zcx.qshop.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.CommodityAssessAdapter;
import com.zcx.qshop.conn.JsonEvaluateAsyGet;
import com.zcx.qshop.conn.JsonEvaluateaddAsyPost;
import com.zcx.qshop.conn.JsonOrderAsyGet;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.view.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2015/9/3 0003.
 */
public class CommodityAssessActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.commodity_assess_list_view) private ListView listView;

    @BoundView(R.id.commodity_assess_confirm_assess) private View confirm;

    private JsonOrderAsyGet.Info.Order order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_commodity_assess);

        order = (JsonOrderAsyGet.Info.Order) getIntent().getSerializableExtra("Order");

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("商品评价");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        new JsonEvaluateAsyGet(order.id, new AsyCallBack<List<Good>>(){

            @Override
            public void onSuccess(String toast, int type, final List<Good> goods) throws Exception {

                listView.setAdapter(new CommodityAssessAdapter(CommodityAssessActivity.this, goods));

                confirm.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        JSONArray array = new JSONArray();

                        for (int i = 0; i < goods.size(); i++) {

                            Good good = goods.get(i);

                            try{

                                if(!good.start.equals("") || !good.content.equals("")){

                                    try {

                                        array.put(new JSONObject().put("gid", good.pid).put("start", good.start).put("content", good.content));

                                    } catch (JSONException e) {

                                        e.printStackTrace();

                                    }

                                }

                            }catch (Exception e){}

                        }

                        new JsonEvaluateaddAsyPost(QSApplication.QSPreferences.readUid(), order.id, array.toString(), new AsyCallBack() {

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                if (MyOrderActivity.onOrderChangeListener != null) {

                                    MyOrderActivity.onOrderChangeListener.onOrderChange();

                                }

                                finish();

                            }

                        }).execute(CommodityAssessActivity.this);

                    }

                });

            }

        }).execute(this);

    }

}
